package com.summer.learning.api;

import com.summer.commonException.XueChengException;
import com.summer.entity.XcUser;
import com.summer.learning.dto.XcChooseCourseDto;
import com.summer.learning.dto.XcCourseTablesDto;
import com.summer.learning.service.LearningService;
import com.summer.model.dto.RestResponse;
import com.summer.utils.CurrentLoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class LeaningController {

    @Autowired
    private LearningService learningService;

    /**
     * 加入课程表接口
     * @param courseId 课程id
     * @return 返回是否添加成功
     */
    @PostMapping("choosecourse/{courseId}")
    public XcChooseCourseDto add2MyCourse(@PathVariable long courseId){
        // 获取用户id
        XcUser loginUser = CurrentLoginUser.getLoginUser();
        if (loginUser == null){
            XueChengException.cast("请先登录");
        }
        String userId = loginUser.getId();
        return learningService.add2MyCourse(userId, courseId);
    }

    /**
     * 获取学习资格接口
     * @param courseId courseId
     * @return dto
     */
    @PostMapping("/choosecourse/learnstatus/{courseId}")
    public XcCourseTablesDto getLearnstatus(@PathVariable("courseId") Long courseId) {
        //登录用户
        XcUser loginUser = CurrentLoginUser.getLoginUser();
        if(loginUser == null){
            XueChengException.cast("请先登录");
        }
        String userId = loginUser.getId();
        return  learningService.getLeanringStatus(userId, courseId);

    }

    @GetMapping("/open/learn/getvideo/{courseId}/{teachplanId}/{mediaId}")
    public RestResponse<String> getvideo(@PathVariable("courseId") Long courseId,
                                         @PathVariable("courseId") Long teachplanId,
                                         @PathVariable("mediaId") String mediaId) {
        //登录用户
        XcUser loginUser = CurrentLoginUser.getLoginUser();

        String userId = null;
        if(loginUser != null){
            userId = loginUser.getId();
        }
        //获取视频
        return learningService.getVideo(userId, courseId, teachplanId, mediaId);
    }

}
